<?php
/*
 * Created on Sep 13, 2010
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
 
include_once("./configure/configure.php");		//--> global var
include_once("Smarty.class.php");				//--> out template
include_once("Common.php");
include_once("UploadFiles.php");				//--> UploadFiles
include_once("Strings.php");					//--> String utils

include_once("customerSession.inc.php");
include_once("Session.php");					//-- Session

include_once("orders/Cart.php");					//--> Cart
include_once("orders/CartProduct.php");				//--> Cart
include_once("orders/CartProductAttribute.php");	//--> Cart

$common 		= new Common();
$objStrings 	= new Strings();		

$objSession		= new Session(DB_TAG_SYSTEM, SESSION_TABLE_NAME);	//-- session
$objCart 		= new Cart();		//--> Cart

session_start();

if($_SESSION['cart'])
	$objCart = unserialize($_SESSION['cart']);
else
	$objCart = new Cart();	//--> Cart
	
$isLogin	= false;

if($objSession->exist()) {
	$isLogin	= true;
}

/*----- out html -----*/
$smarty = new Smarty();	//-- out template
$smarty->template_dir 	= TEMPLATE_DIR;
$smarty->compile_dir  	= COMPILE_DIR;
$smarty->cache_dir  	= CACHE_DIR;

//$smarty->force_compile 	= true;
$smarty->debugging 		= false;
$smarty->caching 		= CACHING;
$smarty->cache_lifetime = CACHE_LIFETIME;

$smarty->assign('objCart', $objCart);
$smarty->assign('objStrings', $objStrings);

if(!$smarty->isCached('index.html')){
	include_once("includeCategory.php");	//--> include category
	
	$promoProducts		= array();
	$allproducts 	= $common->listCustom(DB_TAG_PUBLIC, 'p.id, p.price, s.descript, pd.name, pd.description', "products p, products_description pd, promotion s WHERE p.id = s.productId AND p.status = 'normal' AND p.id = pd.productID AND pd.languageID = $LANGEUAGE_ID", 'ORDER BY p.id', $page, 12);
	foreach($allproducts as $key => $value){
		$arrImages = $common->getHash(DB_TAG_PUBLIC, "SELECT u.id, u.extName FROM upload_files u, product_images p WHERE u.id = p.imageID AND p.productID = ".$value['id']." AND type = " . UploadFiles::TYPE_IMAGE . ' ORDER BY p.isMain LIMIT 1');
		if(count($arrImages) > 0){
			$value['imageid'] 	= key($arrImages);
			$value['imageExt'] 	= $arrImages[$value['imageid']];
			
		}
		
		$promoProducts[] = $value;
	}
	
	
	$featured		= array();
	$sqlFeatured	= "SELECT p.id, p.price, pd.name, pd.description FROM products p, products_description pd WHERE p.status = 'normal' AND p.isFeatured = 1 AND p.id = pd.productID AND pd.languageID = $LANGEUAGE_ID ORDER BY p.id DESC LIMIT 6";
	$allFeatured 	= $common->getAllData(DB_TAG_PUBLIC, $sqlFeatured);
	
	foreach($allFeatured as $key => $value){
		$arrImages = $common->getHash(DB_TAG_PUBLIC, "SELECT u.id, u.extName FROM upload_files u, product_images p WHERE u.id = p.imageID AND p.productID = ".$value['id']." AND type = " . UploadFiles::TYPE_IMAGE . ' ORDER BY p.isMain LIMIT 1');
		if(count($arrImages) > 0){
			$value['imageid'] 	= key($arrImages);
			$value['imageExt'] 	= $arrImages[$value['imageid']];
			
		}
		
		$featured[] = $value;
	}
	
	
	$products		= array();
	$sqlProducts	= "SELECT p.id, p.price, pd.name FROM products p, products_description pd WHERE p.status = 'normal' AND p.isNew = 1 AND p.id = pd.productID AND pd.languageID = $LANGEUAGE_ID ORDER BY p.status, p.r_ordered DESC, pd.r_viewed LIMIT 6";
	$allproducts 	= $common->getAllData(DB_TAG_PUBLIC, $sqlProducts);
	foreach($allproducts as $key => $value){
		$arrImages = $common->getHash(DB_TAG_PUBLIC, "SELECT u.id, u.extName FROM upload_files u, product_images p WHERE u.id = p.imageID AND p.productID = ".$value['id']." AND type = " . UploadFiles::TYPE_IMAGE . ' ORDER BY p.isMain LIMIT 1');
		if(count($arrImages) > 0){
			$value['imageid'] 	= key($arrImages);
			$value['imageExt'] 	= $arrImages[$value['imageid']];
			
		}
		
		$products[] = $value;
	}

	include_once("includeSpec.php");	//--> include spec
	
	$smarty->assign('HOME_URL', HOME_URL);
	$smarty->assign('HOME_URL_HTTP', HOME_URL);

	$smarty->assign('categorys', $categorys);
	$smarty->assign('topCategory', $topCategory);

	$smarty->assign('featured', $featured);
	$smarty->assign('products', $products);
	$smarty->assign('specProducts', $specProducts);

	$smarty->assign('promoProducts', $promoProducts);

}

$smarty->assign('isLogin', $isLogin);

$smarty->display('index.html');
?>
